package com.metasoft.flex.util {
	
	import com.metasoft.flex.MetasoftULC;
	
	import flash.filesystem.*;
	import flash.xml.XMLDocument;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.xml.SimpleXMLDecoder;
	import mx.utils.Base64Encoder;
	import mx.utils.ObjectProxy;
	
	public class MetasoftUtils {

		public static function getImageURI(val:String):String {

			if(val.indexOf(MetasoftULC.UserImgDir) != -1 ) {
				return MetasoftULC.ContextPath + val;
			} else if(val.indexOf("http") == -1 ) { 
				return MetasoftULC.ContextPath + MetasoftULC.ImageDir + val;
			}
			return val;
				
		}
		
		public static function getResouceURI(val:String):String {

			if (val.indexOf(MetasoftULC.UserImgDir) != -1) 
            	return MetasoftULC.ContextPath + val;
			return MetasoftULC.ContextPath + "/" + val;
		}

        public static function getCSV(val:XMLList):String {
        	
        	if (val == null) return "";
        	if (val.length() == 1) return getCData(val);
        	
        	var csv:String = "";
        	for (var i:int = 0; i < val.length(); i++) {
        		csv += getCData(val[i]) + ",";
        	}
        	return csv.substring(0, csv.length-1);
        }
        
        public static function getCData(val:String):String {
        	
        	if (val == null) return "";
        	if (val.indexOf("CDATA") != -1)
            	return val.substring(val.lastIndexOf("[")+1, val.indexOf("]"));
            else
            	return val;
        }
        
        public static function getStringAsArray(val:String,separator:String):Array {
        	
        	if (val == null || val.length == 0) return new Array(0);
        	return val.split(separator);
        }
        
        public static function getCost(val:String):String {
        	
        	val = getCData(val);
        	if ( val == null || val.length <= 0 ) return "0.0";
        	return val;
        }
        
        public static function getArray(val:XMLList):Array {
        	
        	if (val == null) return new Array(0);
        	var arr:Array;
        	
        	if (val.length() == 1)  {
        		
        		arr = new Array(getCData(val));
        		return arr;
        	}

       		arr = new Array();
        	for (var i:int = 0; i < val.length(); i++) {

        		arr.push(getCData(val[i]));
        	}
        	return arr;
        }
        
        public static function getURI(val:String):String {
        	
        	if (val == null || val.length == 0) return "";
        	if (val.indexOf("http") != -1)
        		return val;
        	else if (val.indexOf("www") != -1)
        		return "http://" + val;
            else if (val.indexOf(MetasoftULC.UserImgDir) != -1) 
            	return MetasoftULC.ContextPath + val;
            else 
            	return MetasoftULC.ContextPath + "/" + val;
        }
        
        public static function getResourceURI(val:String, userId : String):String {
        	
        	if (val == null || val.length == 0) return "";
        	if (val.indexOf("http") != -1)
        		return val;
        	else if (val.indexOf("www") != -1)
        		return "http://" + val;
    		else if (val.indexOf(MetasoftULC.UserImgDir) != -1) 
            	return MetasoftULC.ContextPath + val;
            else
            	return MetasoftULC.ContextPath + MetasoftULC.UserImgDir + userId + "/" + val;
        }

        
        public static function setCData(val:String):String {
        	return "<![CDATA[" + val + "]]>";
        }
        
        public static function writeFile(fileNameWithPath:String, fileContent:String):void {
        	
        	var file:File = file.resolvePath(fileNameWithPath);
        	var fileStream:FileStream = new FileStream();

			fileStream.open(file, FileMode.WRITE);
	    	fileStream.writeUTFBytes(fileContent);
			fileStream.close();
		}
        
        public static function readFile(fileNameWithPath:String):String {
        	
        	var file:File= file.resolvePath(fileNameWithPath);
        	var fileStream:FileStream = new FileStream();
        
        	fileStream.open(file, FileMode.READ);	
        	var fileData:String = new String();
        	fileData = fileStream.readUTFBytes(fileStream.bytesAvailable);
        	fileStream.close();
        	
        	return fileData;
        }
        
        public static function isFileExist(fileName:String):Boolean {
        	
        	var file:File = file.resolvePath(fileName);
        	if (file.exists) return true;
        	
        	return false;
        }
        
        public static function tokenize(val:String, delimeter:String):Array {
        	return val.split(delimeter);
        }
        
        
        /**
        * Method to get the body from a SOAP Envelope
        * */
        public static function getBodyFromSOAPXML(xmlObj:Object):Object{
        	if (xmlObj == null) return null;
        	var xmlStr:String = String(xmlObj);
        	var xmlDoc:XMLDocument = new XMLDocument(xmlStr);
            var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
            var resultObj:Object = decoder.decodeXML(xmlDoc);
            var resultObjP:ObjectProxy = ObjectProxy(resultObj);
            var env:ObjectProxy = resultObjP.Envelope;
            var body:Object = env.Body;
            return body;
        }
        
        public static function getArrayFromCollection(arrayCollection:ArrayCollection):Array {
        	
        	if (arrayCollection == null) return new Array(0);
        	var arr:Array  = new Array();
        	for (var i:int = 0; i < arrayCollection.length; i++) {
        		arr.push(arrayCollection.getItemAt(i));
        	}
        	return arr;
        }
        
        
        public static function buildCredentials( username:String, password:String ):String
        {
            var encoder:Base64Encoder = new Base64Encoder();
            encoder.encode( username + ":" + password );
            return encoder.flush();
        }
        
        public static function escapeHTMLAnchor(htmlString:String):String{
			var anchorStart : int = htmlString.indexOf("<a");
			var anchorEnd : int = htmlString.indexOf("</a>");
			var escapedString :String = "";
			if (anchorStart != -1 && anchorEnd != -1){
				escapedString = htmlString.substring(anchorEnd);
			}else{
				escapedString = htmlString;
			}
			return escapedString;
		}
		
		public static function escapeSequence(escapeString : String) : String {

			var tempCount : int = 0;
			for (tempCount = 0; tempCount < escapeString.length; tempCount++) {

				if (escapeString.match("\n") || escapeString.match("\t")) {

					escapeString	= escapeString.replace("\n", "");
					escapeString	= escapeString.replace("\t", "");
				} else {
					break;
				}
			}
			return escapeString;
		}
		
		public static function priceAsNumber(productPrice:String) : Number {

			var price : Number = 0.0;
	       	if (productPrice == null) {
	       		return 0.0;
	       	} else {
	
	       		var priceString : String = productPrice;
	       		price = Number(priceString.substr(1, priceString.length));
	       	}
	       	return price;
        }
        
        
        public static  function formatFileSize(numSize:Number):String 
        {
			var strReturn:String;
			numSize = Number(numSize / 1000);
			strReturn = String(numSize.toFixed(1) + " " + MetasoftULC.Format_KB);
			if (numSize > 1000) {
				numSize = numSize / 1000;
				strReturn = String(numSize.toFixed(1) + " " + MetasoftULC.Format_MB);
				if (numSize > 1000) {
					numSize = numSize / 1000;
					strReturn = String(numSize.toFixed(1) + " " + MetasoftULC.Format_GB);
				}
			}				
			return strReturn;
		}
		
		public static  function unFormatFileSize(strFileSize:String):int 
		{
			var fileSize : int = 0;
			if (strFileSize != null){
				if (strFileSize.indexOf(MetasoftULC.Format_KB) != -1){
					fileSize =  Number(strFileSize.substr(0,strFileSize.lastIndexOf(MetasoftULC.Format_KB) -1));
					fileSize = fileSize * 1000;
				}else if (strFileSize.indexOf(MetasoftULC.Format_MB) != -1){
					fileSize =  Number(strFileSize.substr(0,strFileSize.lastIndexOf(MetasoftULC.Format_KB) -1));
					fileSize = fileSize * 1000 * 1000;
				}else if (strFileSize.indexOf(MetasoftULC.Format_GB) != -1){
					fileSize =  Number(strFileSize.substr(0,strFileSize.lastIndexOf(MetasoftULC.Format_KB) -1));
					fileSize = fileSize * 1000 * 1000 * 1000;
				}			
			}
			return fileSize;
		}
		
		/**
		 * Method to get the file type basis the extn
		 *
		 * */
		public static  function getFileType(extn : String) : String{
	   	
	   		var fileType : String = "";
	   		if (extn == null) return fileType;
	   		if (extn.indexOf(".") != -1){
	   			extn   = extn.substring(extn.lastIndexOf(".") + 1,extn.length);
	   		}
	   		var i : int = 0;
	   		for ( i = 0; i < MetasoftULC.ImageTypesArray.length; i++ ) {
	   			if (MetasoftULC.ImageTypesArray[i].toString().toUpperCase() == extn.toUpperCase()){
	   				fileType = MetasoftULC.Type_StillImage;
	   				break;
	   			}
	   		}
	   		for ( i = 0; i < MetasoftULC.AudioTypeArray.length && fileType == ""; i++ ) {
	   			if (MetasoftULC.AudioTypeArray[i].toString().toUpperCase() == extn.toUpperCase()){
	   				fileType = MetasoftULC.Type_Audio;
	   				break;
	   			}
	   		}
	   		for ( i = 0; i < MetasoftULC.VideoTypeArray.length && fileType == ""; i++ ) {
	   			if (MetasoftULC.VideoTypeArray[i].toString().toUpperCase() == extn.toUpperCase()){
	   				fileType = MetasoftULC.Type_Video;
	   				break;
	   			}
	   		}
	   		if (fileType == "") fileType = extn;
	   		return fileType;
	   }
	}
}